package nanocast.apps.creator.modules.mediaWindow.model
{
	import com.adobe.utils.XMLUtil;
	import com.adobe.xml.syndication.rss.*;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	import mx.collections.ArrayCollection;
	
	import nanocast.apps.creator.modules.mediaWindow.vo.MediaFileType;
	
	import org.osflash.thunderbolt.Logger;
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	import org.puremvc.as3.multicore.utilities.assetLoader.interfaces.IAssetInfo;
	import org.puremvc.as3.multicore.utilities.assetLoader.model.LoaderProxy;
	
	import uk.co.revisual.puremvc.loader.etypes.ClientTypes;
	

	public class MediaProxy extends Proxy implements IProxy
	{
		public static const NAME:String = "org.puremvc.as3.apps.flex.mediaTile.modules.mediaWindow.model.MediaProxy";
		
		public static const MEDIA_FEED_PARSE_COMPLETE:String = "mediaFeedParseComplete";
		
		//add multiple mrss feed
		private static const FILE_LIST:Array = new Array(
															{url:"assets/mediaWindow/mixercastMediaRss.xml", client:ClientTypes.URLLOADER_CLIENT}
														);
		private var graphicArray:ArrayCollection,
					videoArray:ArrayCollection,
					animationArray:ArrayCollection;
					
		private var loaderProxy:LoaderProxy;
		
		public function MediaProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		override public function onRegister():void
		{
			loaderProxy = facade.retrieveProxy(LoaderProxy.NAME) as LoaderProxy;
			
			graphicArray = new ArrayCollection();
			videoArray = new ArrayCollection();
			animationArray = new ArrayCollection();
		}
				
		public function getFileList():Array
		{
			return FILE_LIST;	
		}
		
		public function load():void
		{
			var asset: IAssetInfo; 
			var numAssets: int = 0;
			var loadList: Array = getFileList();
			
			while(numAssets < loadList.length)
			{
				asset = loaderProxy.getAsset(loadList[numAssets].url as String) as IAssetInfo;
				
				asset.clientType = loadList[numAssets].client;
				asset.addEventListener(Event.COMPLETE, completeHandler);
				asset.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				loaderProxy.addToQue(asset);
				
				numAssets++;			
			}
		}
		
		private function completeHandler(e:Event):void
		{
			var asset:IAssetInfo = e.target as IAssetInfo;
			asset.removeEventListener(Event.COMPLETE, completeHandler);
			asset.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			Logger.info(NAME+ " : completeHandler() : "+ asset.url);
			
			parseAsset(asset);
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void
		{
			var asset:IAssetInfo = e.target as IAssetInfo;
			asset.removeEventListener(Event.COMPLETE, completeHandler);
			asset.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			Logger.error(NAME+ " : ioErrorHandler() ", asset);
		}
		
		private function parseAsset(asset:IAssetInfo):void
		{
			Logger.info(NAME+ " :parseAsset() asset = "+ asset.fileType.toLowerCase());
			var assetFileType:String = asset.fileType.toLowerCase();
			switch(assetFileType)
			{
				case MediaFileType.XML:
					parseXML(asset.getAsset());
					
					break;
					
				default:
					Logger.info(NAME+ " : parseAsset() Could not determine Asset File Type");	
			}	
		}
		
		private function parseXML(data:String):void
		{
			Logger.info(NAME+ " : parseXML() isValidXML = "+ XMLUtil.isValidXML(data));
			
			if(! XMLUtil.isValidXML(data))
			{
				return;		
			}
			
			var rss:MediaRSS = new MediaRSS();
			rss.parse(data);
	
			var items:Array = rss.items;
			
			for each(var item:MediaRSSItem20 in items)
			{
				switch(item.mediaContent.type)
				{
					//accepted video formats
					case MediaFileType.VIDEO_FLV:
						
						videoArray.addItem(item);
						break;
						
					//accepted graphic formats
					case MediaFileType.GRAPHIC_GIF:
					case MediaFileType.GRAPHIC_JPEG:
					case MediaFileType.GRAPHIC_JPG:
					case MediaFileType.GRAPHIC_PNG:
						
						graphicArray.addItem(item);
						break;
					
					//accepted animation formats	
					case MediaFileType.GRAPHIC_SWF:
						
						animationArray.addItem(item);
						break;
						
					default:
						Logger.info(NAME+ " : parseXML() Don't know how to handle media item = "+ item.mediaContent.url);							
				}		
			}
			
			sendNotification(MEDIA_FEED_PARSE_COMPLETE, this);
		}
		
	}
}